package nu.mine.tbje.easyEDT;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;


public class XMLServlet extends HttpServlet {

	    public void init()
	        throws ServletException
	    {
	    }

	    public void doGet( HttpServletRequest req, HttpServletResponse resp )
	        throws ServletException, IOException{

	    	String uri = req.getRequestURI();
	    	String striped = uri.substring(uri.lastIndexOf('/')+1, uri.indexOf(".xml"));
	    	System.out.println(striped);
	    	int id = 870;
	    	int week = 0;
	    	try {
				id = Integer.parseInt(striped.substring(striped.indexOf("Groupe")+6, striped.indexOf("Week")));
				week = Integer.parseInt(striped.substring(striped.indexOf("Week")+4));
			} catch (Exception e1) {
				e1.printStackTrace();
		        resp.setContentType("text/html");

		        PrintWriter writer = null;
		        try
		        {
		            writer = resp.getWriter();
		            writer.write("Invalid url");
		        }
		        catch (IOException e)
		        {
		            e.printStackTrace();
		        }
		        finally
		        {
		            writer.flush();
		            writer.close();
		        }
			}

			// Set the content type
	        resp.setContentType("application/octet-stream");

	        // Generate the HTML
	        XMLWriter.getWeek(week, id, resp.getOutputStream());

	    }

	    private String getOutput()
	    {
	        StringBuffer buffer = new StringBuffer();
	        try {
				buffer.append("hei");
			} catch (NullPointerException e) {
			}

	        return buffer.toString();
	    }
	}

